package ru.usu.gv.utils.splines;

import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;

import java.util.Random;

/**
 * An edge view which extrudes a tube out along a spline
 */

public class SplineUtilities
{
	public static final int SEGMENT_SIZE = 10;

	public static Point3d[] draw(Point3d[] tubePoints)
	{
		/**
		 * Creates a key frame using the given inputs. 
		 *
		 * @param k knot value for this key frame 
		 * @param l the linear flag (0 - Spline Interp, 1, Linear Interp
		 * @param pos the position at the key frame
		 * @param q the rotations at the key frame
		 * @param s the scales at the key frame
		 * @param t tension (-1.0 < t < 1.0)
		 * @param c continuity (-1.0 < c < 1.0)
		 * @param b bias (-1.0 < b < 1.0)
		 */

		TCBKeyFrame[] frames = new TCBKeyFrame[tubePoints.length];

		Random rnd = new Random(12345);
		for (int i = 0; i < frames.length; i++)
		{
			Point3f tr = new Point3f(tubePoints[i]);
			//frames[i] = new TCBKeyFrame(rnd.nextFloat(), 0, tr, new Quat4f(0f, 0.0f, 0.0f, 0.0f), new Point3f(0, 0, 0), -0.95f, 0.0f, 0);
			frames[i] = new TCBKeyFrame(rnd.nextFloat(), 0, tr, new Quat4f(0f, 0.0f, 0.0f, 0.0f), new Point3f(0, 0, 0), 0.f, 0.0f, 0);
		}

		Point3d[] mas1 = new Point3d[SEGMENT_SIZE * (tubePoints.length - 1)];

		for (int j = 0; j < tubePoints.length - 1; j++)
		{
			CubicSplineSegment css = new CubicSplineSegment(frames[Math.max(j - 1, 0)], frames[j], frames[j + 1], frames[Math.min(j + 2, tubePoints.length - 1)]);
			for (int i = 0; i < SEGMENT_SIZE; i++)
			{
				Vector3f tr = new Vector3f();

				mas1[SEGMENT_SIZE * j + i] = new Point3d();
				css.getInterpolatedPositionVector((float) (i + 1) / (SEGMENT_SIZE + 1), tr);
				mas1[SEGMENT_SIZE * j + i].x = tr.x;
				mas1[SEGMENT_SIZE * j + i].y = tr.y;
				mas1[SEGMENT_SIZE * j + i].z = tr.z;
			}
		}

		return mas1;
	}
}
